using System;
using System.Collections.Generic;
using System.Text;
namespace CommandLib.Base
{
	public class BCDHelper
	{
		public static byte[] StrToBCD(string str)
		{
			return BCDHelper.StrToBCD(str, str.Length);
		}
		public static byte[] StrToBCD(string str, int numlen)
		{
			while (str.Length < numlen)
			{
				str = "0" + str;
			}
			return BCDHelper.ByteArrayToBCD(Encoding.ASCII.GetBytes(str), 0, numlen);
		}
		public static byte[] ByteArrayToBCD(byte[] NumBitByte, int offset, int numlen)
		{
			int num = (numlen + 1) / 2 - 1;
			byte[] array = new byte[num + 1];
			array[num] = 0;
			for (int num2 = 0; num2 <= numlen - 1; num2++)
			{
				byte b = 0;
				if (num2 + offset >= NumBitByte.Length)
				{
					b = 0;
				}
				else
				{
					b = Convert.ToByte((int)(NumBitByte[num2 + offset] - 48));
				}
				if (num2 % 2 == 0)
				{
					array[num2 / 2] = Convert.ToByte((int)(array[num2 / 2] & 15) | ((int)b << 4 & 255));
				}
				else
				{
					array[num2 / 2] = Convert.ToByte((int)((array[num2 / 2] & 240) | b));
				}
			}
			return array;
		}
		public static int bcdToInt(byte[] bcdNum, int offset, int numlen)
		{
			return Convert.ToInt32(BCDHelper.bcdToString(bcdNum, offset, numlen));
		}
		public static string bcdToString(byte[] bcdNum, int offset, int numlen)
		{
			string text = "";
			int num2;
			int num = Math.DivRem(numlen, 2, out num2);
			if (num2 != 0)
			{
				num++;
			}
			for (int i = 0; i < num; i++)
			{
				string arg_42_0 = text;
				int num3 = (bcdNum[i + offset] & 240) >> 4;
				text = arg_42_0 + num3.ToString("x");
				string arg_5F_0 = text;
				num3 = (int)(bcdNum[i + offset] & 15);
				text = arg_5F_0 + num3.ToString("x");
			}
			return text.ToLower().Replace("f", "");
		}
		public static DateTime Bcd3ToDateTime(byte[] data, int offset)
		{
			int year = Convert.ToInt32("20" + BCDHelper.bcdToString(data, offset, 2));
			int month = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 1, 2));
			int day = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 2, 2));
			return new DateTime(year, month, day);
		}
		public static DateTime Bcd5ToDateTime(byte[] data, int offset)
		{
			int year = Convert.ToInt32("20" + BCDHelper.bcdToString(data, offset, 2));
			int month = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 1, 2));
			int day = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 2, 2));
			int hour = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 3, 2));
			int minute = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 4, 2));
			return new DateTime(year, month, day, hour, minute, 0);
		}
		public static DateTime Bcd6ToDateTime(byte[] data, int offset)
		{
			int year = Convert.ToInt32("20" + BCDHelper.bcdToString(data, offset, 2));
			int month = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 1, 2));
			int day = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 2, 2));
			int hour = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 3, 2));
			int minute = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 4, 2));
			int second = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 5, 2));
			return new DateTime(year, month, day, hour, minute, second);
		}
		public static DateTime Bcd7ToDateTime(byte[] data, int offset)
		{
			int year = Convert.ToInt32(BCDHelper.bcdToString(data, offset, 4));
			int month = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 2, 2));
			int day = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 3, 2));
			int hour = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 4, 2));
			int minute = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 5, 2));
			int second = Convert.ToInt32(BCDHelper.bcdToString(data, offset + 6, 2));
			return new DateTime(year, month, day, hour, minute, second);
		}
		public static byte[] DateTimeToBcd3(DateTime dt)
		{
			List<byte> list = new List<byte>();
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder arg_28_0 = stringBuilder;
			int num = dt.Year - 2000;
			arg_28_0.Append(num.ToString("D2"));
			StringBuilder arg_43_0 = stringBuilder;
			num = dt.Month;
			arg_43_0.Append(num.ToString("D2"));
			StringBuilder arg_5E_0 = stringBuilder;
			num = dt.Day;
			arg_5E_0.Append(num.ToString("D2"));
			list.AddRange(BCDHelper.StrToBCD(stringBuilder.ToString()));
			return list.ToArray();
		}
		public static byte[] DateTimeToBcd5(DateTime dt)
		{
			List<byte> list = new List<byte>();
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder arg_28_0 = stringBuilder;
			int num = dt.Year - 2000;
			arg_28_0.Append(num.ToString("D2"));
			StringBuilder arg_43_0 = stringBuilder;
			num = dt.Month;
			arg_43_0.Append(num.ToString("D2"));
			StringBuilder arg_5E_0 = stringBuilder;
			num = dt.Day;
			arg_5E_0.Append(num.ToString("D2"));
			StringBuilder arg_79_0 = stringBuilder;
			num = dt.Hour;
			arg_79_0.Append(num.ToString("D2"));
			StringBuilder arg_94_0 = stringBuilder;
			num = dt.Minute;
			arg_94_0.Append(num.ToString("D2"));
			list.AddRange(BCDHelper.StrToBCD(stringBuilder.ToString()));
			return list.ToArray();
		}
		public static byte[] DateTimeToBcd6(DateTime dt)
		{
			List<byte> list = new List<byte>();
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder arg_28_0 = stringBuilder;
			int num = dt.Year - 2000;
			arg_28_0.Append(num.ToString("D2"));
			StringBuilder arg_43_0 = stringBuilder;
			num = dt.Month;
			arg_43_0.Append(num.ToString("D2"));
			StringBuilder arg_5E_0 = stringBuilder;
			num = dt.Day;
			arg_5E_0.Append(num.ToString("D2"));
			StringBuilder arg_79_0 = stringBuilder;
			num = dt.Hour;
			arg_79_0.Append(num.ToString("D2"));
			StringBuilder arg_94_0 = stringBuilder;
			num = dt.Minute;
			arg_94_0.Append(num.ToString("D2"));
			StringBuilder arg_AF_0 = stringBuilder;
			num = dt.Second;
			arg_AF_0.Append(num.ToString("D2"));
			list.AddRange(BCDHelper.StrToBCD(stringBuilder.ToString()));
			return list.ToArray();
		}
		public static byte[] DateTimeToBcd7(DateTime dt)
		{
			List<byte> list = new List<byte>();
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder arg_22_0 = stringBuilder;
			int num = dt.Year;
			arg_22_0.Append(num.ToString("D4"));
			StringBuilder arg_3D_0 = stringBuilder;
			num = dt.Month;
			arg_3D_0.Append(num.ToString("D2"));
			StringBuilder arg_58_0 = stringBuilder;
			num = dt.Day;
			arg_58_0.Append(num.ToString("D2"));
			StringBuilder arg_73_0 = stringBuilder;
			num = dt.Hour;
			arg_73_0.Append(num.ToString("D2"));
			StringBuilder arg_8E_0 = stringBuilder;
			num = dt.Minute;
			arg_8E_0.Append(num.ToString("D2"));
			StringBuilder arg_A9_0 = stringBuilder;
			num = dt.Second;
			arg_A9_0.Append(num.ToString("D2"));
			list.AddRange(BCDHelper.StrToBCD(stringBuilder.ToString()));
			return list.ToArray();
		}
	}
}
